<?php
namespace app\wechat\controller;
use app\wechat\middlewares\PersonalMiddlewares;
use app\wechat\middlewares\Response;
use app\models\Bases;
use app\Service\Sms;
class Personal{
    public function home(){
        try{
            $uid = input('param.uid');
            $result = PersonalMiddlewares::homeFactory($uid);
            if($result['code'] != 200){
                throw new \LogicException($result['errmsg'],$result['code']);
            }
            return Response::data($result['code'],$result['errmsg'],$result['data']);
        }catch(\Exception $e){
            return Response::data($e->getCode(),$e->getMessage());
        }
    }

    public function editNickname(){
        try{
            $params = input('param.');
            $result = PersonalMiddlewares::editNicknameFactory($params);
            if($result['code'] != 200){
                throw new \LogicException($result['errmsg'],$result['code']);
            }
            return Response::data($result['code'],$result['errmsg'],$result['data']);
        }catch(\Exception $e){
            return Response::data($e->getCode(),$e->getMessage());
        }
    }

    public function editPwd(){
        try{
            $params = input('param.');
            $result = PersonalMiddlewares::editPwdFactory($params);
            if($result['code'] != 200){
                throw new \LogicException($result['errmsg'],$result['code']);
            }
            return Response::data($result['code'],$result['errmsg'],$result['data']);
        }catch(\Exception $e){
            return Response::data($e->getCode(),$e->getMessage());
        }
    }

    public function editImportantPwd(){
        try{
            $params = input('param.');
            $result = PersonalMiddlewares::editImportantPwdFactory($params);
            if($result['code'] != 200){
                throw new \LogicException($result['errmsg'],$result['code']);
            }
            return Response::data($result['code'],$result['errmsg'],$result['data']);
        }catch(\Exception $e){
            return Response::data($e->getCode(),$e->getMessage());
        }
    }

    public function editAvatar(){
        try{
            $uid = input('param.uid');
            $where['uid'] = ['eq',$uid];
            $file = request()->file('image');
            $info = $file->validate(['size'=>10240,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'avatar');
            if($info){
                $value['avatar'] = $info->getFilename();
            }else{
                throw new \LogicException($file->getError(),10010);
            }
            $field = 'uid,avatar';
            $userModule = new Bases('user');
            @$userInfo = $userModule->find($where,$field);
            $path = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/avatar/';
            if(!empty($userInfo['avatar'])){
                removeImg($path,$userInfo['avatar']);
            }
            @$change = $userModule->setField($where,$value);
            if($change == false){
                throw new \LogicException('操作失败',10011);
            }
            return Response::data(200,'success');
        }catch(\Exception $e){
            return Response::data($e->getCode(),$e->getMessage());
        }
    }
}